package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.WYAccountDaoI;
import sy.model.WYAccount;
import sy.util.Encrypt;
@Repository("wyAccountDao")
public class WYAccountDaoImpl implements WYAccountDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(WYAccount wyAccount) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(wyAccount);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from WYAccount where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public void modify(WYAccount wyAccount) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(wyAccount);
	}

	@Override
	public WYAccount getWYAccountById(String id) {
		// TODO Auto-generated method stub
		String hql = "from WYAccount where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("id",id);
		List<WYAccount> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WYAccount> listWYAccount() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from WYAccount").list();
	}

	@Override
	public WYAccount getLogin(String workCode, String password) {
		// TODO Auto-generated method stub
		String hql = "from WYAccount where workCode = :workCode and password = :password";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("workCode",workCode);
		query.setString("password",password);
		List<WYAccount> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void uodateLoginTime(String token) {
		// TODO Auto-generated method stub
		String hql = "update WYAccount set loginTime = null where token = :token";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("token",token);
		query.executeUpdate();
	}

	@Override
	public WYAccount getWYAccountByToken(String token) {
		// TODO Auto-generated method stub
		String hql = "from WYAccount where token = :token";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("token",token);
		List<WYAccount> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void modifyPassword(String workCode, String password) {
		// TODO Auto-generated method stub
		String hql = "update WYAccount set password = :password where workCode = :workCode";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("workCode",workCode);
		query.setString("password",password);
		query.executeUpdate();
	}

	@Override
	public WYAccount getWYAccountByCondition(String workCode, String telephone) {
		// TODO Auto-generated method stub
		String hql = "from WYAccount where workCode = :workCode ";
		if(!"".equals(telephone)){
			hql += " and name = :telephone";
		}
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("workCode",workCode);
		
		if(!"".equals(telephone)){
			hql += " and name = :telephone";
			query.setString("telephone",telephone);
		}
		List<WYAccount> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}
}
